#ifndef __DBROLE_CREATERECIEVER_H__
#define __DBROLE_CREATERECIEVER_H__

#include "gwmessagetab.hpp"
#include "IMessageOperator.h"
#include "protonet.hpp"
#include "../DBServerTab.h"
#include "boost/uuid/uuid.hpp"
#include "boost/uuid/uuid_io.hpp"
#include "boost/uuid/uuid_generators.hpp"
#include "sqlquery/InsertRoleDetail.h"
#include "dbserver.pb.h"
#include "../Sender/DBRole_Create_FailedSender.h"
#include "../Sender/DBRole_Create_SuccessSender.h"

/**
*@file       DBRole_CreateReciever.h
*@brief      process message DBRole_Create
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/3/28
*/

///process message
class OnDBRole_Create
{
public:
    ///@brief    process message
    ///param[in] op            operator type
    ///param[in] session_id    identifier of session
    ///param[in] pMsgBuffer    message buffer
    ///param[in] msg_bytes     byte length of message buffer
    ///retval    void
    static void OnMapping(IMessageOperator* op, int session_id, void* pMsgBuffer, unsigned long msg_bytes)
    {
        //TODO
        CreateRole param;
        if(ProtoNet<CreateRole>::CreateFromBytes(param, pMsgBuffer, msg_bytes))
        {
            boost::uuids::string_generator generator;
            boost::uuids::uuid role_code_gen = generator(param.rolename());
            std::stringstream role_code;
            role_code << role_code_gen;

            std::stringstream stream;
            if(param.detail().SerializeToOstream(&stream))
            {
                InsertRoleDetail::stmt->setString(1, param.rolename());
                InsertRoleDetail::stmt->setInt(2, param.usrid());
                InsertRoleDetail::stmt->setString(3, role_code.str());
                InsertRoleDetail::stmt->setString(4, param.rolename());
                InsertRoleDetail::stmt->setInt(5, param.level());
                InsertRoleDetail::stmt->setInt(6, param.jobtype());
                InsertRoleDetail::stmt->setInt(7, param.gender());
                InsertRoleDetail::stmt->setInt(8, param.graphics());
                InsertRoleDetail::stmt->setInt(9, param.world());
                InsertRoleDetail::stmt->setBlob(10, &stream);

                int error = EnterFailed::eErrorCode_MIN - 1;
                if(InsertRoleDetail::stmt->executeUpdate() > 0)
                {
                    msgDBRole_Create_Success msg(session_id, param.logincode(), role_code.str(), param.rolename(), param.level(), 
                        param.jobtype(), param.world(), param.gender(), param.graphics());

                    gwMessagePort::ProcessMessage(&msg);
                }
                else
                {
                    msgDBRole_Create_Failed msg(session_id, param.logincode(), CreateRoleFailed::RoleName_Already_Exists);
                    gwMessagePort::ProcessMessage(&msg);
                }

                InsertRoleDetail::stmt->clearParameters();
            }
        }
    }
};

///[message group]  [message type]  [function table] [handler]
RegisterMessageTab(DBServerTypes, DBRole_Create, DBServerTab, OnDBRole_Create);
#endif
